package com.tanhua.dubbo.api;


import com.mongodb.client.result.UpdateResult;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
@DubboService
public class CommentApiImpl implements CommentApi{
    @Autowired
   private MongoTemplate mongoTemplate;
    @Override
    public List<Comment> findComments(String movementId, CommentType comment, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(comment.getType())).skip((page-1)*pagesize)
                .limit(pagesize).with(Sort.by(Sort.Order.desc("content")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public Integer save(Comment comments) {
        Movement movement = mongoTemplate.findById(comments.getPublishId(), Movement.class);
        if (movement!=null){
            comments.setPublishUserId(movement.getUserId());
        }
        mongoTemplate.save(comments);
        Query query = Query.query(Criteria.where("id").is(comments.getPublishId()));
        Update update = new Update();
        if (comments.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }if (comments.getCommentType()==CommentType.LOVE.getType()){
            update.inc("loveCount",1);
        }if (comments.getCommentType()==CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement Modify = mongoTemplate.findAndModify(query, update, Movement.class);
        return Modify.statisCount(comments.getCommentType());
    }

    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType comment) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(comment.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class);
    }

    @Override
    public Integer delete(Comment comment) {
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
         mongoTemplate.remove(query,Comment.class);
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        return modify.statisCount(comment.getCommentType() );
    }

    @Override
    public List<Comment> hasCount(ObjectId comentsid, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("id").is(comentsid);

        Query query = Query.query(criteria);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;


    }

    @Override
    public void updatalikeCount(String comentsid) {
        Query query = Query.query(Criteria.where("id").is(comentsid));
        Update update = new Update();
        update.inc("likeCount",1);
        UpdateResult updateResult = mongoTemplate.updateMulti(query,update,Comment.class);
        System.out.println("修改后的数据"+updateResult);

    }

    @Override
    public void updatalikeCountj(String comentsid) {
        Query query = Query.query(Criteria.where("id").is(comentsid));
        Update update = new Update();
        update.inc("likeCount",-1);
        UpdateResult updateResult = mongoTemplate.updateMulti(query,update,Comment.class);
        System.out.println("修改后的数据2"+updateResult.getMatchedCount());
      }
}
